<template>
  <Select ref="deviceS" placeholder="请选择设备" :value="value" @on-change="change" :disabled="disabled" :multiple="multiple" :transfer="true" filterable clearable>
    <Option v-for="(option, index) in deviceOptions" :value="option.value" :key="`vehicle-complete-option-${option.value}`">{{option.label}}</Option>
    <ul v-if="deviceOptions.length <= 0" class="ivu-select-not-found"><li>暂无数据</li></ul>
  </Select>
</template>

<script>
  import {post}  from '@/util/request'

  export default {
    props: ["value",'multiple','orgIds','disabled','transfer','showBind'], // orgIds:string
    data() {
      return {
        deviceOptions: []
      }
    },
    mounted(){
      this.queryDevices(this.orgId);
    },
    watch:{
      value(v){
        if(!v){
          this.$refs.deviceS.clearSingleSelect();
        }
      },
      orgIds(v){
        this.queryDevices(v);
      }
    },
    computed:{
      isShowBind(){
        return this.showBind === undefined || this.showBind === null || this.showBind === true;
      }
    },
    methods:{
      change(v){
        this.$emit("input", v);
        this.$emit("on-change", v);
      },
      queryDevices(orgId){
        let query = { orgIds:orgId||'' };
        post('/common/findDeviceByOrgId', query).then(res => {
          if (res.data.success){
            let list = res.data.data.map(item => {
              return {
                value: item.deviceCode,
                label: item.deviceCode,
                deviceStatus: item.deviceStatus
              };
            });
            if (!this.isShowBind){
              list = list.filter(o => o.deviceStatus !== 1 || o.value === this.value);
            }
            this.deviceOptions = list;
          }
        });
      }
    }
  }
</script>
